package com.zz.apk.receiver

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.zz.apk.dialog.ProgressDialogFragment
import com.zz.apk.service.DownloadService
import com.zz.framework.core.base.BaseActivity
import com.zz.framework.core.ext.loge
import com.zz.framework.core.ext.showToast
import com.zz.apk.utils.FilesUtil
import com.zz.framework.R
import java.io.File

/**
 * Copyright © 2020 zanezhao All rights reserved.
 * Created by zanezhao on 2020-02-21 21:43.
 * @author: zanezhao 1820022519@qq.com
 * @version: V1.0
 */
class MessageReceiver: BroadcastReceiver() {

    var mProgressDialogFragment: ProgressDialogFragment? = null
    override fun onReceive(context: Context?, intent: Intent?) {
        if (intent?.action == DownloadService.DOWNLOAD_SERVICE_ACTION) {
            loge("receiver DOWNLOAD_SERVICE_ACTION " + intent.getStringExtra(DownloadService.DOWNLOAD_TAG))
            if (intent.getStringExtra(DownloadService.DOWNLOAD_TAG) == DownloadService.TAG_APK) {
                loge("receiver DOWNLOAD_SERVICE_ACTION downloadApk")
                downloadApk(context as BaseActivity, intent)
            }else if (intent.getStringExtra(DownloadService.DOWNLOAD_TAG) == DownloadService.TAG_FILE){
                downloadFile(context as BaseActivity, intent)
            }
        }
    }

    private fun downloadApk(context: BaseActivity, intent: Intent) {
        when (intent.getIntExtra(DownloadService.DOWNLOAD_STATUS, 0)) {
            DownloadService.STATUS_PREPARE -> {
                mProgressDialogFragment =
                    ProgressDialogFragment()
                mProgressDialogFragment?.show(context)
                loge("STATUS_PREPARE show")
            }
            DownloadService.STATUS_PROGRESS -> {
                mProgressDialogFragment?.let {
                    it.update(
                        intent.getIntExtra(DownloadService.DOWNLOAD_PROGRESS, 0),
                        intent.getLongExtra(DownloadService.DOWNLOAD_DOWNLOAD_SIZE, 0),
                        intent.getLongExtra(DownloadService.DOWNLOAD_TOTAL_SIZE, 0)
                    )
                    loge("STATUS_PROGRESS update")
                    if (!it.isVisible) {
                        it.show(context)
                        loge("STATUS_PROGRESS show")
                    }
                }

            }
            DownloadService.STATUS_SUCCESS -> {
                loge("STATUS_SUCCESS")
                mProgressDialogFragment?.dismiss()
                context.getString(R.string.toast_prepare_install).showToast()
                FilesUtil.install(context, intent.getStringExtra(DownloadService.DOWNLOAD_PATH))
            }
            DownloadService.STATUS_FAILED -> {
                loge("STATUS_FAILED")
                mProgressDialogFragment?.dismiss()
                context.getString(R.string.toast_download_failed).showToast()
            }
            DownloadService.STATUS_EXISTS -> {
                loge("STATUS_EXISTS")
                context.getString(R.string.toast_prepare_install).showToast()
                FilesUtil.install(context, intent.getStringExtra(DownloadService.DOWNLOAD_PATH))
            }
        }
    }
    private fun downloadFile(context: BaseActivity, intent: Intent) {
        when (intent.getIntExtra(DownloadService.DOWNLOAD_STATUS, 0)) {
            DownloadService.STATUS_PREPARE -> {
                mProgressDialogFragment = ProgressDialogFragment()
                mProgressDialogFragment?.show(context)
                loge("STATUS_PREPARE show")
            }
            DownloadService.STATUS_PROGRESS -> {
                mProgressDialogFragment?.let {
                    it.update(
                        intent.getIntExtra(DownloadService.DOWNLOAD_PROGRESS, 0),
                        intent.getLongExtra(DownloadService.DOWNLOAD_DOWNLOAD_SIZE, 0),
                        intent.getLongExtra(DownloadService.DOWNLOAD_TOTAL_SIZE, 0)
                    )
                    loge("STATUS_PROGRESS update")
                    if (!it.isVisible) {
                        it.show(context)
                        loge("STATUS_PROGRESS show")
                    }
                }

            }
            DownloadService.STATUS_SUCCESS -> {
                loge("STATUS_SUCCESS")
                mProgressDialogFragment?.dismiss()
                context.getString(R.string.toast_download_success).showToast()
                FilesUtil.openFile(context, File(intent.getStringExtra(DownloadService.DOWNLOAD_PATH)))
            }
            DownloadService.STATUS_FAILED -> {
                loge("STATUS_FAILED")
                mProgressDialogFragment?.dismiss()
                context.getString(R.string.toast_download_failed).showToast()
            }
            DownloadService.STATUS_EXISTS -> {
                loge("STATUS_EXISTS")
                context.getString(R.string.toast_prepare_install).showToast()
                FilesUtil.install(context, intent.getStringExtra(DownloadService.DOWNLOAD_PATH))
            }
        }
    }

}